<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Table_CommodityComment_AddColumn_ToUserId_MasterUserId extends CI_Migration
{
    public function up()
    {
        $fields = array(
            'to_user_id'     => array('type' => 'INT', 'constraint' => '9'),
            'master_user_id' => array('type' => 'INT', 'constraint' => '9'),
        );
        $this->dbforge->add_column('commodity_comment', $fields);

        $this->db->from('commodity_comment');
        $query = $this->db->get();
        $result = $query->result();
        foreach ($result as $row) {
            $comment = CommodityComment_Model::getCommodityCommentByRow($row);
            $fatherComment = CommodityComment_Model::getCommodityCommentById($comment->getFatherId());
            $commodity = Commodity_Model::getCommodityById($comment->getCommodityId());
            if ($fatherComment)
                $comment->setToUserId($fatherComment->getUserId());
            if ($commodity)
                $comment->setMasterUserId($commodity->getUserId());
            CommodityComment_Model::update($comment);
        }
    }

    public function down()
    {
        $this->dbforge->drop_column('commodity_comment', 'to_user_id');
        $this->dbforge->drop_column('commodity_comment', 'master_user_id');
    }
} 